package com.fire.rabbitmq.consumer;

import com.fire.rabbitmq.queue.QueueRouter;
import com.fire.rabbitmq.queue.TtlQueue;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author ：Mr.Fire
 * @date ：Created in 2021/4/25 16:15
 * @description：
 * @modified By：
 * @version: $
 */
@Component
public class Consumer8 {

    @RabbitListener(queues = QueueRouter.QUEUE_DIRECT_A)
    @RabbitHandler
    public void receiveA(Message msg, Channel channel) throws IOException {
        try {
            //消息确认机制还可以起到限流作用，比如在接收到此条消息时休眠几秒钟
            Thread.sleep(3000);
            // 确认收到消息，消息将被队列移除
            // false只确认当前consumer一个消息收到，true确认所有consumer获得的消息。
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Consumer8-direct-A收到确认消息："+msg);
    }


    @RabbitListener(queues = QueueRouter.QUEUE_DIRECT_A)
    @RabbitHandler
    public void receiveAA(Message msg, Channel channel, @Headers Map<String,Object> map) throws IOException {
        try {
            Thread.sleep(3000);
            if (map.get("errorMsg")!=null){
                // 失败确认，
                // multiple：是否批量. true：将一次性拒绝所有小于deliveryTag的消息
                // requeue：是否重新入队列
                channel.basicNack(msg.getMessageProperties().getDeliveryTag(),false,true);
            }
            channel.basicAck(msg.getMessageProperties().getDeliveryTag(),false);
            System.out.println("Consumer8-direct-AA收到确认消息："+msg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
//
//    @RabbitListener(queues = QueueRouter.QUEUE_DIRECT_B)
//    @RabbitHandler
//    public void receiveB(Message msg, Channel channel) throws IOException {
//        try {
//            Thread.sleep(3000);
//            // 确认收到消息，消息将被队列移除
//            // false只确认当前consumer一个消息收到，true确认所有consumer获得的消息。
//            channel.basicAck(
//                    msg.getMessageProperties().getDeliveryTag(),false);
//            System.out.println("Consumer8-direct-B收到确认消息："+msg);
//        } catch (InterruptedException e) {
//            Map<String, Object> headers = msg.getMessageProperties().getHeaders();
//            //重新发送到MQ中
//            AMQP.BasicProperties basicProperties = new AMQP.BasicProperties().builder().contentType("text/plain").headers(headers).build();
//            channel.basicPublish(msg.getMessageProperties().getReceivedExchange(),
//                    msg.getMessageProperties().getReceivedRoutingKey(),basicProperties
//                    msg.getBody());
//            e.printStackTrace();
//        }
//
//    }


}
